<?php

class UserController extends Controller
{
	public $layout="//layouts/nolayout";
	
	public function actionGetTeacherList()
	{
		$userList=User::model()->findAll(array(
			'select'=>array('user_id','name','account','PID','role_id'),
			'condition'=>'role_id!=:role_id',
			'params'=>array(':role_id'=>'1'),
		));
		$json=array();
		for($i=0;$i<count($userList);$i++)
		{
			$elem=$userList[$i]->attributes;
			$elem['role']=$userList[$i]->Role->name;
			$elem['role_id']=$userList[$i]->Role->role_id;
			if (!empty($userList[$i]->Logs))
			{
				$elem['log_time']=$userList[$i]->Logs[count($userList[$i]->Logs)-1]->log_time;
			}
			else
			{
				$elem['log_time']='从未登陆';
			}
			array_push($json,$elem);
		}
		
		echo CJSON::encode($json);
	}
	
	public function actionGetRoleList()
	{
		$roleList=Role::model()->findAll(array(
			'condition'=>'priv!=:adminPriv',
			'params'=>array(':adminPriv'=>'255'),
		));
		echo CJSON::encode($roleList);
	}
	
	public function actionAddRole()
	{
		if (isset($_POST['name']))
		{
			$newRole=new Role;
			$newRole->name=$_POST['name'];
			$newRole->priv = 1;
			$newRole->status = 1;
			if ($newRole->save()>0)
			{
				$json=array(  
						'success'=>true,
						'res'=>$newRole->role_id,
					);   
				echo CJSON::encode($json);
			}else
			{
				$json=array(  
						'success'=>false,
						'res'=>'添加失败！'
					);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'网络错误'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionEditRole()
	{
		if (isset($_POST['role_id']))
		{
			$role=Role::model()->findByPk($_POST['role_id']);
			if (!empty($_POST['name']))
			{
				$role->name=$_POST['name'];
			}else
			{
				$json=array(  
						'success'=>true,
						'res'=>'修改成功！'
					);
				echo CJSON::encode($json);
				return;
			}
				
			if ($role->save()>0)
			{
				$json=array(  
						'success'=>true,
						'res'=>'修改成功！'
					);
				echo CJSON::encode($json);
			}else
			{
				$json=array(  
						'success'=>false,
						'res'=>'修改失败！'
					);   
				echo CJSON::encode($json);
			}
		}else{
			$json=array(  
						'success'=>false,
						'res'=>'网络错误'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionDeleteRole()
	{
		if (isset($_POST['name']))
		{
			$newRole=new Role;
			$newRole->name=$_POST['name'];
			$newRole->priv = 1;
			$newRole->status = 1;
			if ($newRole->save()>0)
			{
				$json=array(  
						'success'=>true,
						'res'=>'添加成功！'
					);   
				echo CJSON::encode($json);
			}else
			{
				$json=array(  
						'success'=>false,
						'res'=>'添加失败！'
					);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'网络错误'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionAddNewUser()
	{
		if (isset($_POST['account'])&&isset($_POST['password'])&&isset($_POST['repeatPassword'])&&isset($_POST['name'])&&isset($_POST['role_id']))
		{
			$newUser=new User;
			$newUser->account=$_POST['account'];
			if ($_POST['password']!=$_POST['repeatPassword'])
			{
				$json=array(  
							'success'=>false,
							'res'=>'两次输入密码不一致'
						);   
				echo CJSON::encode($json);
				return;
			}
			$newUser->password=strtoupper(md5($_POST['password']));
			$newUser->name=$_POST['name'];
			$newUser->PID=$_POST['PID'];
			$newUser->role_id=$_POST['role_id'];
			$newUser->tel=$_POST['tel'];
			if ($newUser->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>$newUser->user_id,
						);   
				echo CJSON::encode($json);
			}else
			{
				$json=array(  
							'success'=>false,
							'res'=>'新增失败！'
						);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'网络错误'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionRegistNewUser()
	{
		if (isset($_POST['account'])&&isset($_POST['password'])&&isset($_POST['repeatPassword'])&&isset($_POST['name']))
		{
			$newUser=new User;
			$newUser->account=$_POST['account'];
			if ($_POST['password']!=$_POST['repeatPassword'])
			{
				$json=array(  
							'success'=>false,
							'res'=>'两次输入密码不一致'
						);
				echo CJSON::encode($json);
				return;
			}
			
			$user=User::model()->findByAttributes(array(
				'account' => $_POST['account'],
			));
			if (!empty($user))
			{
				$json=array(  
							'success'=>false,
							'res'=>'失败！该用户名已经存在！'
						);   
				echo CJSON::encode($json);
				return;
			}
			
			
			$newUser->password=strtoupper(md5($_POST['password']));
			$newUser->name=$_POST['name'];
			$newUser->PID=$_POST['PID'];
			//普通权限
			$newUser->role_id= 0 ;
			$newUser->tel=$_POST['tel'];
			if ($newUser->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>$newUser->user_id,
						);   
				echo CJSON::encode($json);
			}else
			{
				$json=array(  
							'success'=>false,
							'res'=>'新增失败！'
						);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'网络错误'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionEditUser()
	{
		if (isset($_POST['user_id'])&&isset($_POST['name'])&&isset($_POST['role_id']))
		{
			$user=User::model()->findByPk($_POST['user_id']);
			if (empty($user))
			{
				$json=array(  
							'success'=>false,
							'res'=>'失败！对应用户ID不存在！'
						);   
				echo CJSON::encode($json);
				return;
			}
			
			if (!empty($_POST['name']))
				$user->name=$_POST['name'];
			$user->role_id=$_POST['role_id'];
			if (!empty($_POST['PID']))
				$user->PID=$_POST['PID'];
			if ($user->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'修改成功！'
						);   
				echo CJSON::encode($json);
			}else
			{
				$json=array(  
							'success'=>true,
							'res'=>'修改失败！'
						);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'网络错误'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionDeleteUser()
	{
		if (isset($_POST['user_id']))
		{
			$user=User::model()->findByPk($_POST['user_id']);
			if ($user->delete()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'删除成功！'
						);   
				echo CJSON::encode($json);
			}else
			{
				$json=array(  
							'success'=>true,
							'res'=>'删除失败！'
						);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'网络错误'
					);   
			echo CJSON::encode($json);
		}
	}
	
	
	public function actionSetRoleModule($rid,$mid,$status)
	{
		$r_m=new Role_Module;
		$r_m->module_id=$mid;
		$r_m->role_id=$rid;
		if ($status=='true')
		{
			$res=Role_Module::model()->findByAttributes(array('role_id'=>$rid,'module_id'=>$mid));
			if (empty($res))
				$r_m->save();
		}else{
			$res=Role_Module::model()->findByAttributes(array('role_id'=>$rid,'module_id'=>$mid));
			if (!empty($res))
				$res->delete();
		}
	}
	//获取角色与模块绑定的选定状态
	public function actionGetRMChecked($rid)
	{
		$r_mList=Role_Module::model()->findAllByAttributes(array('role_id'=>$rid));
		$json=array();
		for ($i=0;$i<count($r_mList);$i++)
		{
			$elem=array();
			$elem['mid']=$r_mList[$i]->module_id;
			array_push($json,$elem);
		}
		echo CJSON::encode($json);
	}
	
	public function actionChangePasswordPage()
	{
		$this->render('changePasswordPage');
	}
	
	public function actionChagePassword()
	{
		if (isset($_POST['newPassword'])&&isset($_POST['reNewPassword']))
		{
			$user=User::model()->findByPk(Yii::app()->user->id);
			if ($_POST['newPassword']!=$_POST['reNewPassword'])
			{
				$json=array(  
							'success'=>false,
							'res'=>'两次输入密码不一致'
						);   
				echo CJSON::encode($json);
				return;
			}
			
			if (strtoupper(md5($_POST['password']))!=$user->password)
			{
				$json=array(  
							'success'=>false,
							'res'=>'原密码错误'
						);   
				echo CJSON::encode($json);
				return;
			}
			
			$user->password=strtoupper(md5($_POST['newPassword']));
			if ($user->update()>0)
			{
				$json=array(  
								'success'=>true,
								'res'=>'修改成功'
							);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
								'success'=>false,
								'res'=>'数据库错误'
							);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'网络错误'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionShowUserInfoPage()
	{
		$this->render('showUserInfoPage');
	}
	
	public function actionShowRolePage()
	{
		$this->render('showRolePage');
	}
}
